<template>
	<el-dialog title="发送邮件" v-model="visible" :width="1000" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="邮件主题" prop="subject" maxlength="50">
				<el-input v-model="form.subject" placeholder="邮件主题" clearable maxlength="90"></el-input>
			</el-form-item>
			<el-form-item label="收件人" prop="toId" maxlength="50">
				<select-user-dialog v-model:accountId="form.toId" :mode="true"/>
			</el-form-item>
			<el-form-item label="是否外发" prop="sendWebMail">
				<el-checkbox v-model="sendWebMail">外发邮件</el-checkbox>
			</el-form-item>
			<el-form-item label="邮件地址" v-if="sendWebMail">
				<el-input v-model="form.webEmailTo" type="textarea" placeholder='邮件账号用","间隔'></el-input>
			</el-form-item>
			<el-form-item label="邮件内容">
				<sc-editor v-model="form.content" placeholder="请输入" :height="300" clearable  style="width: 100%;" :toolbar="toolbar"></sc-editor>
			</el-form-item>
			<el-form-item label="邮件附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :limit="5" :data="{module:'email'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
			<msg-box-check-box v-model:msgType="form.msgType" code="email"></msg-box-check-box>
		</el-form>
		<template #footer>
			<div style="text-align: center">
				<el-button type="warning" :loading="isSaveing1" @click="save()">保 存</el-button>
				<el-button type="primary" :loading="isSaveing" @click="submit()">发 送</el-button>
			</div>
		</template>
	</el-dialog>
</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import {defineAsyncComponent} from "vue";
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
import AttachMenu from "@/views/module/attachMenu";

const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
	components: {
		msgBoxCheckBox,
		selectUserDialog,
		AttachMenu,
		scEditor,
	},
	emits: ['success', 'closed'],
	props:{
		folderId:String
	},
	data() {
		return {
			apiObj: this.$API.file.attach.uploadFile,
			fileList: [],
			toolbar: ['undo redo | forecolor backcolor bold italic underline strikethrough link | blocks fontfamily fontsize | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | pagebreak | image media table template preview | selectall'],
			isShowMsg:false,
			sendWebMail:false,
			model:"send",
			form: {
				bodyId:"",
				webEmailFlag:"",
				webEmailTo:'',
				toId:"",
				subject:"",
				attachId: "",
				content: "",
				msgType: '',
				sendType:""
			},
			rules: {
				subject: [
					{required: true, message: '邮件主题不能为空', trigger: 'blur'}
				]
			},
			visible: false,
			isSaveing: false,
			isSaveing1:false
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(model) {
			this.visible = true;
			this.model=model;
			return this;
		},
		//表单提交方法
		submit() {
			if(this.form.toId==""&&this.form.webEmailTo=='')
			{
				this.$alert("请选择邮件接收人", "提示", {type: 'error'});
				return;
			}
			if(this.form.toId=="@all")
			{
				this.$alert("内部邮件不可全局发送", "提示", {type: 'error'});
				return;
			}
			if(this.sendWebMail)
			{
				this.form.webEmailFlag = "1"
			}
			this.form.sendType=this.model;
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = await this.$API.email.sendEmail.post(this.form);
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("发送成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		async save() {
			this.isSaveing1 = true;
			var res = await this.$API.email.saveEmail.post(this.form);
			this.isSaveing1 = false;
			if (res.code == 200) {
				this.$emit('success')
				this.visible = false;
				this.$message.success("保存成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		},
		//表单注入数据
		setData(data) {
			this.form.bodyId = data.bodyId
			this.form.toId = data.toId
			this.form.subject = data.subject
			if(this.model=="reply")
			{
				this.form.content = data.content+"<p>----------以下为回复内容---------</p><p></p>"
			}else
			{
				this.form.content = data.content
			}
			this.form.attachId = data.attachId
		},
		revBpmData(data){
			this.form.subject = data.title;
			this.form.content = data.content;
			this.form.attachId = data.attachId;
		}
	}
}
</script>

<style>
</style>
